<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Sticker extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_sticker_model", "table");
        $this->sticker_type="sticker_type";//贴纸分类表
        $this->sticker="sticker";//贴纸表
    }
////////////////////////////////////////////////////////////////贴纸分类开始//////////////////////////////////////////////////////////////////////////
    /**
     * 贴纸分类主页
     */
    public function index(){
        $res=$this->table->all();
        $data['data']=$res;
        $this->layout("no_edit/sticker/index",$data);
    }

    /**
     * 新建分类
     */
    public function add_sticker_type(){
        $this->layout("no_edit/sticker/add_sticker_type");
    }

    /**
     * 保存新建分类
     */
    public function save_add_sticker_type(){
        if(empty($_POST['title'])){
            response(-1,"分类名称不能为空");
        }
        if(mb_strlen($_POST['title'])>10){
            response(-1,"最大为10字");
        }
//        if(empty($_POST['icon1'])){
//            response(-1,"分类图标不能为空");
//        }
//        if(empty($_POST['icon2'])){
//            response(-1,"分类未选中图标不能为空");
//        }
        $data['test_sticker_type_name']=$_POST['title'];
//        $data['test_select_icon']=$_POST['icon1'];
//        $data['test_no_select_icon']=$_POST['icon2'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $data['time_tmp']=$this->getMsecTime();
        $res=$this->table->add($this->sticker_type,$data);
        if($res){
            response(0,"添加成功");
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 编辑分类
     */
    public function edit(){
        $id=$_GET['id'];
        if(empty($id)){
            response(-1,"参数不能为空");
        }
        //查询这个贴纸分类的信息
        $sticker_type=$this->table->one($this->sticker_type,['id'=>$id]);
        $data['data']=$sticker_type;
        $sticker=$this->sticker(1);
        $data['sticker']=$sticker;
        $this->layout("no_edit/sticker/edit",$data);
    }

    /**
     * 保存编辑分类
     */
    public function save_edit(){
        if(empty($_POST['title'])){
            response(-1,"分类名称不能为空");
        }
        if(mb_strlen($_POST['title'])>10){
            response(-1,"最大为10字");
        }
//        if(empty($_POST['icon1'])){
//            response(-1,"分类图标不能为空");
//        }
//        if(empty($_POST['icon2'])){
//            response(-1,"分类未选中图标不能为空");
//        }
        $id=$_POST['id'];
        $data['test_sticker_type_name']=$_POST['title'];
//        $data['test_select_icon']=$_POST['icon1'];
//        $data['test_no_select_icon']=$_POST['icon2'];
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->sticker_type,$data,['id'=>$id]);
        if($res){
            response(0,"修改成功");
        }else{
            response(0,"修改失败");
        }
    }

    /**
     * 贴纸分类上移
     */
    public function up_sort(){
        $start_id=$_POST['start'];
        $now_id=$_POST['now'];
        $res=$this->table->up_sort($start_id,$now_id);
        if($res){
            response(0,"上移成功");
        }else{
            response(-1,"上移失败");
        }
    }

    /**
     * 贴纸分类下移
     */
    public function down_sort(){
        $now_id=$_POST['now'];
        $end_id=$_POST['end'];
        $res=$this->table->down_sort($end_id,$now_id);
        if($res){
            response(0,"下移成功");
        }else{
            response(-1,"下移失败");
        }
    }

    /**
     * 贴纸分类启用
     */
    public function uses(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->sticker_type,['test_status'=>2],['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 贴纸分类停用
     */
    public function stop(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->sticker_type,['test_status'=>1],['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

///////////////////////////////////////////////////////////////////////贴纸分类结束////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////贴纸开始/////////////////////////////////////////////////////////////////////////////////

    /**
     * 保存贴纸
     */
    public function add_sticker(){
        if(empty($_POST['title'])){
            response(-1,"贴纸名称不能为空");
        }
        if(empty($_POST['icon3'])){
            response(-1,"分类图标不能为空");
        }
        if(empty($_POST['icon4'])){
            response(-1,"分类素材图标不能为空");
        }
        if(mb_strlen($_POST['title'])>15){
            response(-1,"最大字数为15字");
        }
        $data['sticker_id']=$_POST['id'];
        $data['sticker_name']=$_POST['title'];
        $data['icon']=$_POST['icon3'];
        $data['pics']=$_POST['icon4'];
        $data['sorts']=$this->getMsecTime();
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add($this->sticker,$data);
        if($res){
            $res1=$this->sticker(2,$data['sticker_id']);
//            response(0,"添加成功",$res1);
            response(0,"添加成功",base_url("no_edit/sticker/edit?id={$data['sticker_id']}"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 贴纸修改
     */
    public function sticker_add(){
        if(empty($_POST['sticker_id'])){
            response(-1,"缺少参数");
        }
        if(empty($_POST['icon'])){
            response(-1,"分类图标不能为空");
        }
        if(empty($_POST['pics'])){
            response(-1,"分类素材图标不能为空");
        }
        if(empty($_POST['sticker_name'])){
            response(-1,"分类名称不能为空");
        }
        if(mb_strlen($_POST['sticker_name'])>15){
            response(-1,"最大字数为15字");
        };
        $data['icon']=$_POST['icon'];
        $data['pics']=$_POST['pics'];
        $data['sticker_name']=$_POST['sticker_name'];
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->sticker,$data,['id'=>$_POST['sticker_id']]);
        if($res){
            response(0,"保存成功");
        }else{
            response(-1,"保存失败");
        }
    }

    /**
     * 贴纸停用
     */
    public function sticker_stop(){
        if(empty($_POST['sticker_id'])){
            response(-1,"缺少参数");
        }
        $data['del4']=1;
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->sticker,$data,['id'=>$_POST['sticker_id']]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 贴纸启用
     */
    public function sticker_start(){
        if(empty($_POST['sticker_id'])){
            response(-1,"缺少参数");
        }
        $data['del4']=2;
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->sticker,$data,['id'=>$_POST['sticker_id']]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 贴纸上移
     */
    public function sticker_up(){
        $now_id=$_POST['sticker_id'];
        $start_id=$_POST['start'];
        $res=$this->table->sticker_up($start_id,$now_id);
        if($res){
            response(0,"上移成功");
        }else {
            response(-1, "上移失败");
        }
    }

    /**
     * 贴纸下移
     */
    public function sticker_down(){
        $now_id=$_POST['sticker_id'];
        $end_id=$_POST['end'];
        $res=$this->table->sticker_down($end_id,$now_id);
        if($res){
            response(0,"下移成功");
        }else {
            response(-1, "下移失败");
        }
    }

///////////////////////////////////////////////////////////////////////贴纸结束/////////////////////////////////////////////////////////////////////////////////

    /**
     * 发布贴纸分类
     */
    public function fabu(){
        $res=$this->table->fabu();
        if($res){
            response(0,"发布成功");
        }else{
            response(-1,"发布失败");
        }
    }

    /**
     * 查询贴纸分类下的贴纸
     */
    public function sticker($type=1,$id=""){
        if($type==1){
            $id=$_GET['id'];
            $res=$this->table->sticker($id);
            return $res;
        }else if($type==2){
            $res=$this->table->sticker($id);
            return $res;
        }
    }

    /**
     * 获取毫秒级别的时间戳
     */
    public function getMsecTime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}